package test;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Locale;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

public class RunServer {
	public static void main(String[] args) {

		try {
			Locale.setDefault(new Locale("en"));
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
//		        if ("Nimbus".equals(info.getName())) {
//		            UIManager.setLookAndFeel(info.getClassName());
//		            break;
//		        }
//		    }
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			int port = Integer.parseInt(JOptionPane.showInputDialog("port",
					"12345"));


			ServerSocket serverSocket = new ServerSocket(port);
			String key = JOptionPane
					.showInputDialog("server please input key:");
			String serverName = JOptionPane.showInputDialog("Vui lòng nhập tên");
			TrayIcon trayIcon = null;
			if (SystemTray.isSupported()) {

				SystemTray tray = SystemTray.getSystemTray();
				Image image = Toolkit.getDefaultToolkit().getImage(
						RunServer.class.getResource("/test/a.png"));

				MouseListener mouseListener = new MouseListener() {

					public void mouseClicked(MouseEvent e) {
					}

					public void mouseEntered(MouseEvent e) {
					}

					public void mouseExited(MouseEvent e) {
					}

					public void mousePressed(MouseEvent e) {
					}

					public void mouseReleased(MouseEvent e) {
					}
				};

				ActionListener exitListener = new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						System.exit(0);
					}
				};

				PopupMenu popup = new PopupMenu();
				MenuItem defaultItem = new MenuItem("Exit");
				defaultItem.addActionListener(exitListener);
				popup.add(defaultItem);
				
				MenuItem copyThread = new MenuItem("Copy");

				trayIcon = new TrayIcon(image, "Nhan chatter", popup);
				trayIcon.setToolTip("N_Chatter_Server_Port:" + port + "k:"
						+ key);
				
				popup.add(copyThread);
				
				copyThread.addActionListener(new ThreadCopy(trayIcon, key));

				ActionListener actionListener = new ActionListener() {
					@SuppressWarnings("null")
					public void actionPerformed(ActionEvent e) {
						// TrayIcon trayIcon = null;
						// trayIcon.displayMessage("Action Event",
						// "An Action Event Has Been Performed!",
						// TrayIcon.MessageType.INFO);
					}
				};

				trayIcon.setImageAutoSize(true);
				trayIcon.addActionListener(actionListener);
				trayIcon.addMouseListener(mouseListener);

				try {
					tray.add(trayIcon);
				} catch (AWTException e) {
					System.err.println("TrayIcon could not be added.");
				}

			} else {

				// System Tray is not supported

			}
			trayIcon.displayMessage("Chatter server",
					"Server started successfully", TrayIcon.MessageType.INFO);

			ThreadAutoGC threadAutoGC = new ThreadAutoGC();
			threadAutoGC.start();

			while (true) {
				Socket socket = serverSocket.accept();
				MyThread thread = new MyThread(socket);
				thread.key = key;
				thread.serverName = serverName;
				thread.start();
			}



		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage(), "error",
					JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		}
	}
}
